package com.mystyle.le.weekproject.fragment;


import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.mystyle.le.weekproject.R;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {


    private RadioGroup rg_main;
    private ViewPager viewPager;
    private List<Fragment> list = new ArrayList<Fragment>();
    private String[]titles = {"精品","排行"};
    private RadioButton[] rbs ;

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initView(view);
        //初始化ViewPager
        initViewPager();
        //初始化导航书签
        initTab();

        return view;
    }

    private void initTab() {
        rbs = new RadioButton[titles.length];
        for (int i =0;i<titles.length;i++){
            rbs[i] = new RadioButton(getActivity());
            rbs[i].setGravity(Gravity.CENTER);
            Bitmap a = null;
            rbs[i].setButtonDrawable(new BitmapDrawable(a));

            rbs[i].setText(titles[i]);
            rbs[i].setBackgroundResource(R.drawable.color_home);
            int screenWith = getResources().getDisplayMetrics().widthPixels;
            int eachWith = screenWith/titles.length;
            rbs[i].setWidth(eachWith);
            rg_main.addView(rbs[i]);
        }
        rbs[0].setChecked(true);//默认第一个是选中的
        rg_main.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i =0;i<titles.length;i++){
                    if (rbs[i].getId()==checkedId){
                        //Viewpager滑动到 点击的RadioButton上
                        viewPager.setCurrentItem(i);
                    }
                }
            }
        });
    }

    private void initViewPager() {
        BoutiqueFragment boutiqueFragment = new BoutiqueFragment();
        SeniorityFragment seniorityFragment = new SeniorityFragment();
        list.add(boutiqueFragment);
        list.add(seniorityFragment);
        viewPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager()));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                rbs[position].setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initView(View view) {
        rg_main = ((RadioGroup) view.findViewById(R.id.rg_main));
        viewPager = ((ViewPager) view.findViewById(R.id.viewPager));

    }

    class MyFragmentPagerAdapter extends FragmentPagerAdapter{

        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }

}
